-
Notifications
You must be signed in to change notification settings - Fork 12
gitconfig: improve rbm alias base detection #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Let rbm resolve master/main from local refs first, then fall back to the remote default branch chosen via origin/HEAD (including remote-only refs). Forward all user-supplied arguments to git rebase so the alias mirrors the native command’s behavior.
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @PeterDaveHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enhances the rbm git alias to intelligently detect the main development branch (master/main) and support rebasing onto remote branches when local ones don't exist. The alias now checks the remote's default branch via origin/HEAD first, then falls back to local branch detection, and properly forwards all user arguments to the underlying git rebase command.
Key changes:
- Adds remote default branch detection using
symbolic-refonorigin/HEAD - Supports falling back to remote-only branches (e.g.,
origin/master) when local branches are missing - Fixes argument forwarding by replacing
$1with"$@"to pass all user-supplied flags and arguments
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| rbc = rebase --continue | ||
| rbi = rebase -i | ||
| rbm = "!fn(){ branch=''; if git show-ref --verify --quiet refs/heads/master; then branch='master'; elif git show-ref --verify --quiet refs/heads/main; then branch='main'; fi; if [ -n '$branch' ]; then git rebase $branch $1; else echo 'Both master and main branch not found!' >&2; return 1; fi; };fn" | ||
| rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn" |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message 'Both master and main branch not found!' is misleading when the actual logic checks for remote branches beyond just master/main (via remote_branch). Update the message to reflect that no suitable base branch was found, not just that master/main are missing.
| rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn" | |
| rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ]; then echo 'No suitable base branch found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn" |
PR Code Suggestions ✨Explore these optional code suggestions:
|
|||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request improves the rbm git alias to more robustly detect the base branch for rebasing. The new logic correctly forwards all arguments to git rebase. However, the shell script logic is overly complex and has a counter-intuitive order of precedence for branch detection. I've suggested a simplified and more logical implementation that prioritizes the remote's default branch (origin/HEAD) before falling back to local master or main branches. This makes the alias more readable, maintainable, and predictable.
| rbc = rebase --continue | ||
| rbi = rebase -i | ||
| rbm = "!fn(){ branch=''; if git show-ref --verify --quiet refs/heads/master; then branch='master'; elif git show-ref --verify --quiet refs/heads/main; then branch='main'; fi; if [ -n '$branch' ]; then git rebase $branch $1; else echo 'Both master and main branch not found!' >&2; return 1; fi; };fn" | ||
| rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The shell script for the rbm alias is quite complex and has a confusing order of precedence for branch detection. It could be simplified for better readability and maintainability, while also making the logic more intuitive.
The current logic prioritizes a local master or main branch over the remote's default branch if the default branch is named something other than master or main (e.g., develop). This seems to contradict the goal of detecting the base branch from origin/HEAD first.
Here's a simplified version that has a clearer order of precedence:
- Check
origin/HEADfor the default branch. Prefer a local tracking branch if it exists, otherwise use the remote branch. - If not found, fall back to checking for a local
masterbranch. - If still not found, fall back to checking for a local
mainbranch. - If no branch is found, print an error.
This revised script is shorter, easier to understand, and more robustly follows the intended logic of prioritizing the remote's default branch. I've also updated the error message to be more accurate.
!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); if [ -n "$remote_head" ]; then remote_branch=${remote_head#origin/}; if [ "$remote_branch" != "$remote_head" ]; then if git show-ref --verify --quiet "refs/heads/$remote_branch"; then base=$remote_branch; elif git show-ref --verify --quiet "refs/remotes/$remote_head"; then base=$remote_head; fi; fi; fi; if [ -z "$base" ]; then if git show-ref --verify --quiet refs/heads/master; then base=master; elif git show-ref --verify --quiet refs/heads/main; then base=main; fi; fi; if [ -z "$base" ]; then echo 'Could not determine default branch (main, master, or from origin/HEAD).' >&2; return 1; fi; git rebase "$base" "$@"; };fn
|
@codex review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| rbc = rebase --continue | ||
| rbi = rebase -i | ||
| rbm = "!fn(){ branch=''; if git show-ref --verify --quiet refs/heads/master; then branch='master'; elif git show-ref --verify --quiet refs/heads/main; then branch='main'; fi; if [ -n '$branch' ]; then git rebase $branch $1; else echo 'Both master and main branch not found!' >&2; return 1; fi; };fn" | ||
| rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve rebase options before inserting base branch
The alias now forwards "$@" to git rebase but it prepends the resolved base as the first argument. Git parses options before positional arguments, so running git rbm --autostash or git rbm -i executes git rebase main --autostash, which treats --autostash as a branch name and aborts with fatal: invalid refname. This fails to deliver the stated goal of mirroring git rebase behaviour and breaks any use of the alias with rebase options. The base branch needs to be appended after user options (e.g. git rebase "$@" "$base") to keep flags working.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| rbc = rebase --continue | ||
| rbi = rebase -i | ||
| rbm = "!fn(){ branch=''; if git show-ref --verify --quiet refs/heads/master; then branch='master'; elif git show-ref --verify --quiet refs/heads/main; then branch='main'; fi; if [ -n '$branch' ]; then git rebase $branch $1; else echo 'Both master and main branch not found!' >&2; return 1; fi; };fn" | ||
| rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Respect origin/HEAD before falling back to local master
Line 34: when origin/HEAD points to a branch that isn’t master or main (e.g. trunk), but a stale local master branch still exists, we never reach the remote default fallback—the refs/heads/master check triggers first and forces every git rbm to rebase onto master. This regresses the very case this change set is meant to handle: repositories that renamed their default branch but kept master around. To reproduce, set origin/HEAD to origin/trunk, keep a local master, and run git rbm; it rebases onto master instead of origin/trunk.
Please move the generic remote_branch fallback ahead of the local master/main checks so we always honor the true default branch before considering legacy names.
- rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn"
+ rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn" | |
| rbm = "!fn(){ base=''; remote_head=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true); remote_branch=${remote_head#origin/}; if [ -n \"$remote_branch\" ] && [ \"$remote_branch\" != \"$remote_head\" ]; then case \"$remote_branch\" in master|main) if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif git show-ref --verify --quiet \"refs/remotes/origin/$remote_branch\"; then base=origin/$remote_branch; fi;; esac; fi; if [ -z \"$base\" ] && [ -n \"$remote_branch\" ]; then if git show-ref --verify --quiet \"refs/heads/$remote_branch\"; then base=$remote_branch; elif [ -n \"$remote_head\" ] && git show-ref --verify --quiet \"refs/remotes/$remote_head\"; then base=$remote_head; fi; fi; if [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/master; then base=master; elif [ -z \"$base\" ] && git show-ref --verify --quiet refs/heads/main; then base=main; fi; if [ -z \"$base\" ]; then echo 'Both master and main branch not found!' >&2; return 1; fi; git rebase \"$base\" \"$@\"; };fn" |
User description
Let
rbmresolve master/main from local refs first, then fall back to the remote default branch chosen via origin/HEAD (including remote-only refs). Forward all user-supplied arguments to git rebase so the alias mirrors the native command’s behavior.GitHub Copilot Summary:
PR Type
Enhancement
Description
Improved
rbmalias to detect default branch fromorigin/HEADfirstFalls back to local
master/mainbranches if remote default unavailableSupports remote-only branches when local equivalents don't exist
Forwards all user arguments to
git rebasecommandDiagram Walkthrough
File Walkthrough
gitconfig
Improve rbm alias branch detection logicgitconfig
rbmalias to resolve default branch fromorigin/HEADsymbolicreference first
master/mainbranches if remotedefault unavailable
don't exist
user-supplied arguments via
"$@"Summary by CodeRabbit
rbmGit alias for improved base branch detection. Now intelligently derives the base branch from remote origin HEAD with fallback logic to master or main branches, providing more robust handling of edge cases and remote branch naming.